package com.lunevich.json.adapter;

import java.util.List;

import com.lunevich.json.R;
import com.lunevich.json.helpers.DownloadManager;
import com.lunevich.json.wrappers.TwitterWrapper;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public abstract class CommonAdapter extends BaseAdapter {
	
	private List<TwitterWrapper> twitterList;
	private Context context;
	
	public CommonAdapter(Context context, List<TwitterWrapper> twitterList) {
		this.context = context;
		this.twitterList = twitterList;
	}

	@Override
	public int getCount() {
		return twitterList.size();
	}

	@Override
	public TwitterWrapper getItem(int position) {
		return twitterList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.row, parent, false);
		}
		ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
		TextView fromUserName = (TextView)convertView.findViewById(R.id.userName);
		
		String url = getItem(position).getProfileImageUrl();
		String userName = getAuthorName(getItem(position));
		
		fromUserName.setText(userName);
		DownloadManager.getImageDownloader(context).download(url, imageView, this);
		return convertView;
	}
	
	public abstract String getAuthorName(TwitterWrapper twitterWrapper);
}